package guhong.play.commandsystem.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.util.StrUtil;
import guhong.play.commandsystem.CommandManager;
import guhong.play.commandsystem.constant.Constant;
import guhong.play.commandsystem.dto.entity.Command;
import guhong.play.commandsystem.job.CommandJob;
import guhong.play.commandsystem.util.print.PrintUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * 工具类
 *
 * @author : 李双凯
 * @date : 2019-11-20 22:32
 **/
@Data
public class ToolUtil {


    private static ExecutorService threadPoolExecutor = new ThreadPoolExecutor(5, 10,
            3L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(3), new ThreadFactoryBuilder().build(), new ThreadPoolExecutor.AbortPolicy());


    /**
     * 异步运行一个工作
     *
     * @param command    命令
     * @param commandJob 工作
     */
    public static void asynchStart(Command command, CommandJob commandJob) {
        threadPoolExecutor.execute(() -> {
            try {
                commandJob.run(command);
            } catch (Exception e) {
                PrintUtil.printError(command, e);
            }
        });
    }

    /**
     * 参数值是否为空
     * 用于判断命令参数的值是否为空字符串
     *
     * @param paramValue 参数值
     * @return 是空字符串则为true
     */
    public static Boolean isBlankParam(String paramValue) {
        if (paramValue.matches("\"\\s*\"")) {
            return true;
        }
        if (paramValue.matches("'\\s*'")) {
            return true;
        }
        if ("\"\"".equals(paramValue)) {
            return true;
        }
        if ("''".equals(paramValue)) {
            return true;
        }
        return false;
    }


    /**
     * 获得环境变量的值
     *
     * @param key key
     * @return 返回环境变量的值
     */
    public static String getEnv(String key) {
        Map<String, String> envMap = System.getenv();
        return envMap.get(key);
    }

    /**
     * 获得盘符
     *
     * @param path 路径
     * @return 返回路径所在的盘符
     */
    public static String getDrive(String path) {
        return path.substring(0, path.indexOf(":"));
    }


    /**
     * 是否是int
     *
     * @param str 字符串
     * @return 是返回true
     */
    public static boolean isInteger(String str) {
        if (StrUtil.isBlank(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public static String getHeadInfo() {
        String userName = System.getProperty("user.name");
        String now = DateUtil.format(new Date(), "yyyy-MM-dd HH-mm");
        String defaultCommand = CommandManager.getSystemConfig().getDefaultCommand();
        return "\n" + userName + "#" + now +  "|"+defaultCommand+"|=>: ";
    }



    /**
     * 对象组中是否全是 Empty Object
     *
     * @param os
     * @return
     */
    public static boolean isAllEmpty(Object... os) {
        for (Object o : os) {
            if (isNotEmpty(o)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 对象是否不为空(新增)
     *
     * @param o String,List,Map,Object[],int[],long[]
     * @return
     */
    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    /**
     * 对象是否为空
     *
     * @param o String,List,Map,Object[],int[],long[]
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if (o.toString().trim().equals("")) {
                return true;
            }
        } else if (o instanceof List) {
            if (((List) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Map) {
            if (((Map) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Set) {
            if (((Set) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Object[]) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof int[]) {
            if (((int[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof long[]) {
            if (((long[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof BigDecimal) {
            BigDecimal bigDecimal = (BigDecimal) o;
            return bigDecimal.equals(BigDecimal.ZERO);
        }
        return false;
    }

    /**
     * 对象组中是否存在 Empty Object
     *
     * @param os 对象组
     * @return
     */
    public static boolean isOneEmpty(Object... os) {
        for (Object o : os) {
            if (isEmpty(o)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获得Pom中的版本号项目版本
     * @param projectPath 项目路径
     */
    public static String getPomVersion(String projectPath) {
        if (StrUtil.isBlank(projectPath)) {
            projectPath = Constant.PROJECT_PATH;
        }
        return XmlOperationUtil.getMavenVersion(projectPath);
    }

    /**
     * 获得项目版本
     */
    public static String getProjectVersion() {
        return CommandManager.getSystemConfig().getVersion();
    }

}
